package hive.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Demo01_connectHive {

    public static void studentQuery(){

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            conn = DriverManager.getConnection(
                 "jdbc:hive2://node101:10000/default",
                 "bduser",
                 "root"
            );

            ps = conn.prepareStatement("select * from student where loc like ?");

            ps.setString(1,"%b%");

            rs = ps.executeQuery();

            while(rs.next()){
                System.out.println("id："+rs.getInt(1));
                System.out.println("name："+rs.getString(2));
                System.out.println("loc："+rs.getString(3));
                System.out.println("============================================");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        studentQuery();
    }
}
